package com.second.app.thread.reentrant.b;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author GYZ
 * <pre>
 *  演示 ReentrantLock 错误使用场景 造成
 *  Maximum lock count exceeded 问题
 * </pre>
 */
public class ReentrantLockError {

    public static ReentrantLock reentrantLock = new ReentrantLock();

    public static int num = 5;

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(ReentrantLockError::doTest, "线程" + (i + 1)).start();
        }
    }

    private static void doTest() {
        while (true) {
            reentrantLock.lock();
            if (num>0){
                System.out.println(Thread.currentThread().getName()+" 排队买面包");
                num--;
                System.out.println("面包数量="+num);
            }
        }
    }
}
